﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

using System.Collections;
using System.Configuration;
using System.Data;
using System.IO; 

using System.Web.Security;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Web.Script.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService()]
public class MyWS : System.Web.Services.WebService
{

    public MyWS()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public string GetServerResponse(string callerName)
    {
        string City = "";
        var deal = new DealesDataContext();
        var query = deal.City_ReturnForSubscribe();
        foreach (var item in query)
        {
            City += item.CityID + "]" + item.City + "|";
        }
        if (City.Length > 0)
        {
            City = City.Remove(City.Length - 1, 1);
        }
        if (callerName == string.Empty)
            throw new Exception("Web Service Exception: invalid argument");

        return string.Format(City);
    }

    [WebMethod(EnableSession = true)]
    public string SubscribePopUpBlock(string callerName)
    {
        Session["SubscribeDaialyDeal"] = null;
        if (callerName == string.Empty)
            throw new Exception("Web Service Exception: invalid argument");

        return string.Format("Sucess");
    }


    [WebMethod(EnableSession = true)]
    public string SubscribeUser(string CityName, string CityID, string Email)
    {
        Session["SubscribeDaialyDeal"] = "1"; 
        var sub = new DealesDataContext();
        DateTime UnSubScribeDate = Convert.ToDateTime("1/1/2020");
        var query = sub.Insert_Subscribe(Convert.ToInt64(CityID), Email, true, Convert.ToDateTime(System.DateTime.Now), Convert.ToDateTime(UnSubScribeDate));

        var query1 = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
        string DealID = "0";
        foreach (var item in query1)
        {
            DealID = item.DealID.ToString();
        }

        WebRequest objRequest = System.Net.HttpWebRequest.Create("http://www.zupons.com/Email.aspx?DID=" + DealID);
        StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
        string result = sr.ReadToEnd();
        sr.Close();
        string html = result;


        //System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
        //replacements.Add("<%City%>", CityName.ToString());
        //replacements.Add("<%Email%>", Email.ToString());
        //replacements.Add("<%MailSubject%>", html.ToString());
        //System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl,
        //    Mailer.MessageTemplate.SubscribeUser, replacements, Email.ToString(), "Subscride User");
        //Mailer.InternalSend(confirmation, false);

       

        return string.Format(DealID);
        
    }


  //  [WebMethod(EnableSession = true)]
  //  public string FaceBook(string name, string Email)
    
  //  {
  //      try
  //      {

  //          MembershipCreateStatus S;
  //          MembershipUser newUser = Membership.CreateUser(
  //Email,
  //Email,
  //Email,
  //"Your name?",
  //name,
  //true,
  //out S);

  //          string Message = verifyStatus(S);


  //          var UserID = newUser.ProviderUserKey;// = newUser.ProviderUserKey; lblErr


  //          Session["username"] = name;
  //          System.Web.Security.Roles.AddUserToRole(name, "user");
  //          FormsAuthentication.SetAuthCookie(Email, false);


  //          System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
  //          replacements.Add("<%UserName%>", Email);
  //          replacements.Add("<%FullName%>", name);
  //          replacements.Add("<%Email%>", Email);

  //          System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl, Mailer.MessageTemplate.NewUserRegistrationConfirmation, replacements, Email, "Your Zupons Registration");
  //          Mailer.InternalSend(confirmation, false);





  //          string strData = name;
  //          char[] splitChar = new char[1];
  //          splitChar[0] = ' ';
  //          String[] col = strData.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);

  //          string FName = col[0];
  //          string LName = "";

  //          if (col.Count() > 1)
  //          {
  //              FName = col[0];
  //              LName = col[1];
  //          }

  //          string Address = null;
  //          string City = null;
  //          string State = null;
  //          string ZipCode = null;
  //          string Phone_Home = null;
  //          System.Data.Linq.Binary UserImage = null;
  //          string Phone_Mob = null;
  //          string Phone_Off = null;
  //          string Fax = null;
  //          string CardHolderName = null;
  //          string CardNumber = null;
  //          string BillingAddress = null;
  //          string SecurityCode = null;
  //          string City2 = null;
  //          string State2 = null;
  //          string ExpirationDate = "1/1/1900 12:00:00 AM";
  //          string PostalCode = null;
  //          bool Policy = true;
  //          var reg = new DealesDataContext();
  //          var RegisterQuery = reg.Insert_UserDetails(Convert.ToString(UserID), FName, LName, Address, City, State, ZipCode, UserImage, Phone_Home, Phone_Mob, Phone_Off, Fax, Convert.ToString(UserID), CardHolderName, Convert.ToInt64(CardNumber), BillingAddress, Convert.ToInt64(SecurityCode), City2, State2, Convert.ToDateTime(ExpirationDate), PostalCode, Policy);
  //          return string.Format("Created");
  //      }
  //      catch (Exception ex)
  //      {
  //          Session["username"] = name;
  //          //System.Web.Security.Roles.AddUserToRole(name, "user");
  //          FormsAuthentication.SetAuthCookie(Email, false);
  //          return string.Format("Logged");
  //      }

  //  }
  //  public string verifyStatus(MembershipCreateStatus status)
  //  {
  //      switch (status)
  //      {
  //          case MembershipCreateStatus.DuplicateUserName:
  //              return "Username already exists. Please enter a different user name.";

  //          case MembershipCreateStatus.DuplicateEmail:
  //              return "A username for that e-mail address already exists. Please enter a different e-mail address.";

  //          case MembershipCreateStatus.InvalidPassword:
  //              return "The password provided is invalid. Please enter a valid password value.";

  //          case MembershipCreateStatus.InvalidEmail:
  //              return "The e-mail address provided is invalid. Please check the value and try again.";

  //          case MembershipCreateStatus.InvalidAnswer:
  //              return "The password retrieval answer provided is invalid. Please check the value and try again.";

  //          case MembershipCreateStatus.InvalidQuestion:
  //              return "The password retrieval question provided is invalid. Please check the value and try again.";

  //          case MembershipCreateStatus.InvalidUserName:
  //              return "The user name provided is invalid. Please check the value and try again.";

  //          case MembershipCreateStatus.ProviderError:
  //              return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

  //          case MembershipCreateStatus.UserRejected:
  //              return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

  //          case MembershipCreateStatus.Success:
  //              return "Registeration Successfull";

  //          default:
  //              return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  //      }
  //  }
}

